package com.meilingcloud.biobank.controller;

import com.meilingcloud.biobank.domain.Family;
import com.meilingcloud.biobank.service.IFamilyService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 家系 Controller
 *
 * @author chenl
 * @date 2021-06-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/family")
public class FamilyController extends BaseController {

    private final IFamilyService iFamilyService;

    /**
     * 查询家系 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:family:list')")
    @GetMapping("/list")
    public TableDataInfo list(Family family) {
        startPage();
        List<Family> list = iFamilyService.queryList(family);
        return getDataTable(list);
    }

    /**
     * 获取家系 详细信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:family:query')")
    @GetMapping(value = "/{familyId}")
    public AjaxResult getInfo(@PathVariable("familyId") Long familyId) {
        return AjaxResult.success(iFamilyService.getById(familyId));
    }

    /**
     * 新增家系
     */
    @PreAuthorize("@ss.hasPermi('biobank:family:add')")
    @Log(title = "家系管理 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody Family family) {
        family.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iFamilyService.save(family));
    }

    /**
     * 修改家系
     */
    @PreAuthorize("@ss.hasPermi('biobank:family:edit')")
    @Log(title = "家系管理 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody Family family) {
        family.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iFamilyService.updateById(family));
    }

    /**
     * 删除家系
     */
    @PreAuthorize("@ss.hasPermi('biobank:family:remove')")
    @Log(title = "家系管理 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{familyIds}")
    public AjaxResult remove(@PathVariable Long[] familyIds) {
        return toAjax(iFamilyService.removeByIds(Arrays.asList(familyIds)));
    }

    /**
     * 获取家系图谱
     */
    @PreAuthorize("@ss.hasPermi('biobank:family:query')")
    @GetMapping(value = "getMap/{familyId}")
    public AjaxResult getFamilyMap(@PathVariable("familyId") Long familyId) {
        return AjaxResult.success(iFamilyService.getFamilyMap(familyId));
    }

}
